package com.wolfking.converter.web;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.wolfking.converter.entity.ConverterPage;
import com.wolfking.converter.entity.KvObject;
import com.wolfking.converter.entity.QueryBodyParam;
import com.wolfking.converter.entity.SqlDefine;
import com.wolfking.converter.service.SqlConverterExecuteService;
import com.wolfking.spi.SpiApplicationContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Slf4j
@RestController
@RequestMapping("/template/interface")
@Api(value = "tempalte", tags = "sql模板")
public class TemplateController {


    @Autowired
    private SqlConverterExecuteService sqlConverterExecuteService;

    @Autowired
    private WebResponseConverter webResponseConverter;

    @PostMapping("/queryOne")
    @ApiOperation(value = "查询单条数据")
    public Object queryOne(@RequestBody QueryBodyParam bodyParam) throws Exception {
        try {
            return assemblyResponse(sqlConverterExecuteService.executeOne(bodyParam.getParams()));
        } catch (Exception e) {
            SpiApplicationContext.setSpiCode(DefaultResponseConverter.class.getName());
            return webResponseConverter.convertException(e);
        }
    }

    @PostMapping("/queryList")
    @ApiOperation(value = "查询列表数据")
    public Object queryList(@RequestBody QueryBodyParam bodyParam) throws Exception {
        try {
            return assemblyResponse(sqlConverterExecuteService.executeList(bodyParam.getParams()));
        } catch (Exception e) {
            SpiApplicationContext.setSpiCode(DefaultResponseConverter.class.getName());
            return webResponseConverter.convertException(e);
        }
    }

    @PostMapping("/queryPage")
    @ApiOperation(value = "查询分页数据")
    public Object queryPage(@RequestBody QueryBodyParam bodyParam) throws Exception {
        try {
            return assemblyResponse(sqlConverterExecuteService.executePage(bodyParam.getParams()));
        } catch (Exception e) {
            SpiApplicationContext.setSpiCode(DefaultResponseConverter.class.getName());
            return webResponseConverter.convertException(e);
        }
    }

    private Object assemblyResponse(KvObject<SqlDefine, ?> entry) throws Exception {
        Object value = entry.getValue();
        if (StringUtils.isNotBlank(entry.getKey().getResponseConverter())) {
            SpiApplicationContext.setSpiCode(entry.getKey().getResponseConverter());
        } else {
            SpiApplicationContext.setSpiCode(DefaultResponseConverter.class.getName());
        }
        if (value instanceof JsonArray) {
            return webResponseConverter.convertList((JsonArray) value);
        } else if (value instanceof JsonObject) {
            return webResponseConverter.convertOne((JsonObject) value);
        } else if (value instanceof ConverterPage) {
            return webResponseConverter.convertPage((ConverterPage) value);
        } else if (value instanceof Exception) {
            return webResponseConverter.convertException((Exception) value);
        } else {
            throw new RuntimeException("un know return type " + entry.getValue().getClass());
        }
    }
}
